/*
 ============================================================================
 Name		 : ContactEngine.h
 Author	     : jshuabo.com
 Version	 : 1.0
 Copyright   : jshuabo.com,All Right Reserved!
 Description : CContactEngine declaration
 ============================================================================
 */

#ifndef CONTACTENGINE_H
#define CONTACTENGINE_H

// INCLUDES
#include <e32std.h>
#include <e32base.h>
#include <CNTDB.H>        			 
#include <CNTITEM.H>
#include <CNTFILT.H>
#include <CNTFIELD.H>
#include "Contact.h"
 
#define  WRITE_TO_LOG
//#define  Debug

class TContactItemID;

class MContactExportingNotify
{
public:
  virtual  void ExportingNotify(const TInt aCurrentCount,const TInt aWholeCount)=0;
};



class ContactEngine : public CBase
{
public:	 
	~ContactEngine(); 
	static ContactEngine* NewL(MContactExportingNotify* aContactExportingNotify);	 
	static ContactEngine* NewLC(MContactExportingNotify* aContactExportingNotify); 
	
public:
	void  ExportContacts(const TDesC& aFileName);
	void  RefreshContacts();
	Contact* GetContactByIndex(const TInt aIndex)const;
	TInt  GetContactCount();
	void  DeleteContact(const TInt aIndex);
	
private: 	
	void  ExportingContactsToArray(); 
	void  ExportToFile(const TDesC& aFileName);
	void  ExportASingleContact(Contact& aContact,const TDesC& aFileName);
	
private: 
	ContactEngine();	 
	void  ConstructL(MContactExportingNotify* aContactExportingNotify);	
	void  InitializeContactItemID();
	void  InitializeHeadArray();
	void  ExportContactsWithAPI(const TDesC& aFileName);	
	void  CheckItemID();
	void  SetContactFieldByIndex(CContactItemField* pContactItemField,const TInt aIndex,Contact& pContact);
	
private:
	void 	WriteToLog(const TDesC8& aDesC);	
	static  HBufC8* ReadHeadText(const TDesC& aFileName);
	
	
private:
	CContactDatabase* 				iContactsDb;
	RPointerArray<Contact>			iContactArray;
	RPointerArray<TContactItemID>	iContactIdArray;
	RPointerArray<HBufC>            iHeadArray;
	MContactExportingNotify*        iExportingNotify;          
	HBufC8*                         iHeadText;
};
#endif // CONTACTENGINE_H
